package kz.pompei.ifacer.util;

import java.awt.Color;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Util {
  public static int sqr(Point p1, Point p2) {
    return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
  }
  
  public static Rectangle rectBy2Points(Point p1, Point p2) {
    int x = p1.x;
    if (p2.x < x) x = p2.x;
    int y = p1.y;
    if (p2.y < y) y = p2.y;
    int width = p1.x - p2.x;
    int height = p1.y - p2.y;
    if (width < 0) width = -width;
    if (height < 0) height = -height;
    return new Rectangle(x, y, width, height);
  }
  
  public static String join(String connector, Object... oo) {
    StringBuilder sb = new StringBuilder();
    for (Object o : oo) {
      if (sb.length() > 0) sb.append(connector);
      sb.append(o);
    }
    return sb.toString();
  }
  
  public static String join(String connector, Iterable<?> ss) {
    StringBuilder sb = new StringBuilder();
    for (Object o : ss) {
      if (sb.length() > 0) sb.append(connector);
      sb.append(o);
    }
    return sb.toString();
  }
  
  public static void copy(InputStream in, OutputStream out) {
    copy(in, out, 2048);
  }
  
  public static void copy(InputStream in, OutputStream out, int bufferSize) {
    try {
      byte[] buf = new byte[bufferSize];
      while (true) {
        int read = in.read(buf);
        if (read < 0) break;
        out.write(buf, 0, read);
      }
      in.close();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public static boolean eq(Object o1, Object o2) {
    if (o1 == o2) return true;
    if (o1 == null) return o2 == null;
    if (o2 == null) return false;
    return o1.equals(o2);
  }

  public static Color strToColor(String str) {
    if (str == null) return null;
    str = str.trim().toUpperCase();
    
    {
      if ("BLACK".equals(str)) return Color.BLACK;
      if ("BLUE".equals(str)) return Color.BLUE;
      if ("CYAN".equals(str)) return Color.CYAN;
      if ("DARK_GRAY".equals(str)) return Color.DARK_GRAY;
      if ("GRAY".equals(str)) return Color.GRAY;
      if ("GREEN".equals(str)) return Color.GREEN;
      if ("LIGHT_GRAY".equals(str)) return Color.LIGHT_GRAY;
      if ("MAGENTA".equals(str)) return Color.MAGENTA;
      if ("ORANGE".equals(str)) return Color.ORANGE;
      if ("PINK".equals(str)) return Color.PINK;
      if ("RED".equals(str)) return Color.RED;
      if ("WHITE".equals(str)) return Color.WHITE;
      if ("YELLOW".equals(str)) return Color.YELLOW;
    }
    
    if (str.startsWith("#")) {
      try {
        return Color.decode(str);
      } catch (NumberFormatException e) {}
      return Color.BLACK;
    }
    
    if (str.startsWith("RGB")) {
      str = str.substring(3).trim();
      if (str.startsWith("(")) str = str.substring(1).trim();
      if (str.startsWith(")")) str = str.substring(0, str.length() - 1).trim();
      String[] split = str.split(",");
      if (split.length < 3) return Color.BLACK;
      try {
        int red = Integer.parseInt(split[0].trim());
        int green = Integer.parseInt(split[1].trim());
        int blue = Integer.parseInt(split[2].trim());
        return new Color(red, green, blue);
      } catch (NumberFormatException e) {}
      return Color.BLACK;
    }
    
    return Color.BLACK;
  }

  public static String floatToStr(float value) {
    String ret = "" + value;
    if (ret.contains(".")) {
      while (ret.endsWith("0")) {
        ret = ret.substring(0, ret.length() - 1);
      }
    }
    if (ret.endsWith(".")) {
      ret = ret.substring(0, ret.length() - 1);
    }
    return ret;
  }
}
